import 'dart:convert';

import 'package:flutter_test/flutter_test.dart';
import 'package:im_app/im/dao/ImMessageDao.dart';
import 'package:im_app/im/dao/ImSessionDao.dart';
import 'package:im_app/im/pojo/ImPojo.dart';
import 'package:im_app/im/service/ImMessageService.dart';
import 'package:im_app/im/socket/dto/business/Message.dart';
import 'package:im_app/im/util/DbUtil.dart';
import 'package:im_app/im/util/ImSingleton.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';

void main() {
  test("getMessageGroups2", () async {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
    Database database = await DbUtil.connect("imtest.db");
    ImSingleton.getInstance()?.dataBase = database;

    var sessionDao = new ImSessionDao();
    List<ImSession> result = await sessionDao.getByUserIdAndSize('test00100', 500);
    for (var mg in result) {
      print("name: ${mg.groupCode}, ${mg.toUserId}");
    }
    print("size: " + result.length.toString());

    database.close();
  });

  test("getMessage2", () async {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
    Database database = await DbUtil.connect("im.db");
    ImSingleton.getInstance()?.dataBase = database;

    var messageDao = new ImMessageDao();
    var sessionDao = new ImSessionDao();
    List<ImSession> sessions = await sessionDao.getByUserIdAndSize('test00101', 500);
    for (var session in sessions) {
      int? sessionId = session.id;
      if (sessionId != null) {
        List<ImMessage> result = await messageDao.getNewsBySessionId(sessionId, DateTime.now(), 500);
        // for (var mg in result) {
        //   print("name: ${mg.groupId}, ${mg.toUserId}");
        // }
        if (result.length != 0) {
          print("groupCode: $sessionId , ${session.fromUserId}-${session.toUserId}-${session.groupCode}" );
        }
        // print("size: " + result.length.toString());
      }
    }
    database.close();
  });

  test("getMessage232", () async {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
    Database database = await DbUtil.connect("im.db");
    ImSingleton.getInstance()?.dataBase = database;

    var messageDao = new ImMessageDao();
    List<ImMessage> result = await messageDao.getNewsBySessionId(365, DateTime.now(), 500);
    for (var mg in result) {
      print("name: " + mg.content);
    }
    database.close();
  });

  test("getMessage3", () async {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
    Database database = await DbUtil.connect("im.db");
    ImSingleton.getInstance()?.dataBase = database;

    var messageDao = ImMessageDao();
    List<ImMessage> result = await messageDao.getNewMessages(500);
    for (var mg in result) {
      print("name: " + jsonEncode(mg));
    }
    print("size: " + result.length.toString());
    database.close();
  });

  test("getMessage4", () async {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
    Database database = await DbUtil.connect("imtest.db");
    ImSingleton.getInstance()?.dataBase = database;

    var messageDao = new ImMessageDao();
    List<String> ids = ["test00100"];
    List<ImMessage> result = await messageDao.getByUserIds("test00100", ids);
    for (var mg in result) {
      print("mg " + jsonEncode(mg));
    }
    print("size: " + result.length.toString());
    database.close();
  });

  test("getMessage55", () async {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
    Database database = await DbUtil.connect("imtest.db");
    ImSingleton.getInstance()?.dataBase = database;

    var sessionDao = new ImSessionDao();
    List<ImSession> result = await sessionDao.getByUserIdAndSize('test00100', 500);
    for (var mg in result) {
      int? id = mg.id;
      if (id != null) {
        if (mg.fromUserId == null && mg.toUserId == null && mg.groupCode == null) {
           print("id $id");
        }
      }
    }
    database.close();
  });

  test("getMessage56", () async {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
    Database database = await DbUtil.connect("imtest.db");
    ImSingleton.getInstance()?.dataBase = database;

    var sessionDao = new ImSessionDao();
    ImSession? session = await sessionDao.getById(347);  // 348  351 344 347
    print("message group ${jsonEncode(session)}");
    database.close();
  });

  test("insert message", () async {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
    Database database = await DbUtil.connect("imtest.db");
    ImSingleton.getInstance()?.dataBase = database;

    var messageSerive = new ImMessageService();
    MessageObject messageObject = MessageObject("test00100", "\u{2696}\u{FE0F}asfda大法师", MessageType.RichText);
    await messageSerive.saveOrUpdates("test00100", [messageObject], true);
    database.close();
  });

  test("get message", () async {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
    Database database = await DbUtil.connect("imtest.db");
    ImSingleton.getInstance()?.dataBase = database;

    var messageDao = new ImMessageDao();
    ImMessage? result = await messageDao.getByServerIdAndUserId("00101121", 'test00100');
    print("data: " + jsonEncode(result));
    database.close();
  });
}